package com.aliwo.service.impl;

import com.aliwo.domain.ProductOrder;
import com.aliwo.external.RemoteProductService;
import com.aliwo.service.ProductOrderService;
import com.aliwo.utils.JsonUtil;
import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author xuyayuan
 * @data 2021年07月10日
 */
@Service
public class ProductOrderServiceImpl implements ProductOrderService {
    //@Autowired
    //private RestTemplate restTemplate;
    private static final Logger logger = LoggerFactory.getLogger(ProductOrderServiceImpl.class);

    @Autowired
    private RemoteProductService remoteProductService;

    @Override
    public ProductOrder saveProductOrder(Integer userId, Integer productId) {
        // 通过ribbon 调用商品服务
        //Map<String, Object> map =
        //        restTemplate.getForObject("http://product-service/api/v1/product/getById/"+productId,
        //                Map.class);
        JsonNode jsonNode = JsonUtil.str2JsonNode(remoteProductService.getProductById(productId));
        //Map<String, Object> map = (Map<String, Object>) remoteProductService.findProductById(productId);
        //System.out.println("通过ribbon 调用商品服务 根据商品id (http://product-service/api/v1/product/getById/) :" + map);
        System.out.println("通过ribbon 调用商品服务 根据商品id (http://product-service/api/v1/product/getById/) :" + jsonNode);
        ProductOrder productOrder = new ProductOrder();
        productOrder.setUserId(String.valueOf(userId));
        productOrder.setCreateTime(new Date());
        //productOrder.setProductName(map.get("productName").toString());
        //productOrder.setPrice(Integer.valueOf(String.valueOf(map.get("price"))));
        productOrder.setProductName(jsonNode.get("productName").toString());
        productOrder.setPrice(Integer.valueOf(String.valueOf(jsonNode.get("price"))));
        productOrder.setTradeNo(UUID.randomUUID().toString());
        logger.info("service-order save()");
        return productOrder;
    }
}
